package com.person.leetcode.editor.cn;

import java.util.HashMap;

/**
 * 65.有效数字
 * 2022-05-07 09:58:36
 **/
public class Sequence_0065_ValidNumber {
    public static void main(String[] args) {
        Solution solution = new Sequence_0065_ValidNumber().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        //状态机
        // 数 . + - e E other

//
//        状态 事件  个位数     .       +/-     e/E         other
//        start       int   in_float   sign    end         end
//        int         int   float      end     in_exp      end
//        float      float   end       end     in_exp      end
//        sign       int    in_float   end      end        end
//        in_float   float    end      end      end        end
//        exp         exp     end        end     end         end
//        in_exp    exp     end         in_exp     end         end
//        end
//

        private HashMap<String, String[]> table = new HashMap<String, String[]>() {{
            put("start", new String[]{"int", "in_float", "sign", "end", "end"});
            put("int", new String[]{"int", "float", "end", "in_exp", "end"});
            put("float", new String[]{"float", "end", "end", "in_exp", "end"});
            put("sign", new String[]{"int", "in_float", "end", "end", "end"});
            put("in_float", new String[]{"float", "end", "end", "end", "end"});
            put("exp", new String[]{"exp", "end", "end", "end", "end"});
            put("in_exp", new String[]{"exp", "end", "in_exp", "end", "end"});
            put("end", new String[]{"end", "end", "end", "end", "end"});
        }};

        private StringBuilder resString = new StringBuilder();
        private String state = "start";

        public boolean isNumber(String s) {
            for (int i = 0; i < s.length(); i++) {
                doing(s.charAt(i));
            }

            return !state.startsWith("in_") && s.equals(resString.toString());
        }

        private void doing(char c){
            state = table.get(state)[getCol(c)];
            if (!"end".equals(state)){
                resString.append(c);
            }
        }

        //动作映射
        private int getCol(char c){
            if (Character.isDigit(c)){
                return 0;
            }else if (c == '.'){
                return 1;
            }else if ('+' == c || '-' == c){
                return 2;
            }else if ('e' == c || 'E' == c){
                return 3;
            }else{
                return 4;
            }
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
